package com.hcrazy.manchester.app.services;

import java.util.List;

import org.joda.time.DateTime;

import com.hcrazy.manchester.app.jdbc.ForumDAO;
import com.hcrazy.manchester.app.jdbc.GroupDAO;
import com.hcrazy.manchester.app.jdbc.GroupUserDAO;
import com.hcrazy.manchester.app.pojo.ErrorMsg;
import com.hcrazy.manchester.app.pojo.Forum;
import com.hcrazy.manchester.app.pojo.Group;
import com.hcrazy.manchester.app.pojo.GroupUser;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.utils.Constant;

public class GroupService {

	private ForumDAO forumDAO;
	
	private GroupDAO groupDAO;
	
	private GroupUserDAO groupUserDAO;

	public GroupUserDAO getGroupUserDAO() {
		return groupUserDAO;
	}

	public void setGroupUserDAO(GroupUserDAO groupUserDAO) {
		this.groupUserDAO = groupUserDAO;
	}

	public GroupDAO getGroupDAO() {
		return groupDAO;
	}

	public void setGroupDAO(GroupDAO groupDAO) {
		this.groupDAO = groupDAO;
	}

	public ForumDAO getForumDAO() {
		return forumDAO;
	}

	public void setForumDAO(ForumDAO forumDAO) {
		this.forumDAO = forumDAO;
	}

	public ErrorMsg addOrUpdateGroup(Group group) throws Exception{
		ErrorMsg msg = new ErrorMsg();
		msg.setCode(Constant.SUCCESS);//success
		Forum forum = new Forum();
		forum.setStatus(Constant.FORUM_STATUS_GROUP);
		forum.setName(group.getName());
		forum.setLevel(group.getLevel());
		forum.setSortId(group.getSortId());
		DateTime dt = new DateTime();
		group.setDateline(dt.toString("yyyy-MM-dd hh:mm:ss"));
		if(group.getGroupId() > 0){
			forum.setForumId(group.getGroupId());
			group.setLastupdate(dt.toString("yyyy-MM-dd hh:mm:ss"));
			group.setPraiseCount(group.getPraiseCount() + group.getPraiseBaseCount());
			group.setCommentCount(group.getCommentCount() + group.getCommentBaseCount());
			forumDAO.updateForum(forum);
			groupDAO.updateGroup(group);
		}else{
			if(null != group && group.getPraiseBaseCount() > 0){
				group.setPraiseCount(group.getPraiseBaseCount());
			}
			if(null != group && group.getCommentBaseCount() > 0){
				group.setCommentCount(group.getCommentBaseCount());
			}
			forumDAO.addForum(forum);
			int groupId = forumDAO.getIncreamentId();
			group.setGroupId(groupId);
			groupDAO.addGroup(group);
		}
		
		return msg;
	}
	
	public String delGroup(int forumId) throws Exception{
		//删除本版块
		forumDAO.delForum(forumId);
		//删除群组
		groupDAO.delGroup(forumId);
		
		return null;
	}
	
	public Group getGroupById(int groupId) throws Exception{
		return groupDAO.getGroupById(groupId);
	}
	
	public PageModel getPagedInfos(PageModel model,String search,int sortId) throws Exception{
		return groupDAO.getPagedInfos(model, search, sortId);
	}
	
	public List<Forum> getGroupsBySortId(int sortId) throws Exception{
		if(sortId <= 0){
			return null;
		}
		return forumDAO.getGroupsBySortId(sortId);
	}
	
	public void addGroupUser(GroupUser gUser) throws Exception{
		gUser.setJoindateline(System.currentTimeMillis()/1000);
		gUser.setStatus(Constant.GROUP_USER_APPLY);
		groupUserDAO.addGroupUser(gUser);
	}
	
	public void apply(long groupId,long userId) throws Exception{
		groupUserDAO.apply(groupId, userId);
	}
	
	public void refuse(long groupId,long userId) throws Exception{
		groupUserDAO.refuse(groupId, userId);
	}
	
	public List<GroupUser> getGroupUsers(long groupId,int status) throws Exception{
		return groupUserDAO.getGroupUsers(groupId,status);
	}
}
